package main.java.indi.zyj.hot100;

import java.util.HashMap;

public class _07_lengthOfLongestSubstring {
    public int lengthOfLongestSubstring(String s) {

        int length = s.length();
        if (length == 0){
            return 0;
        }


        int left = 0, maxLen = 0, right = 0;
        HashMap<Character, Integer> window = new HashMap<>();

        while (right < length){

            char rightC = s.charAt(right++);

            // 将字符进入窗口
            window.put(rightC, window.getOrDefault(rightC, 0) + 1);

            // 存在则缩小窗口
            while (window.get(rightC) > 1){
                char leftC = s.charAt(left++);
                window.put(leftC, window.getOrDefault(leftC, 0) - 1);
            }

            maxLen = Math.max(maxLen, right - left);

        }

        return maxLen;

    }
}
